#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
美尔雅（sh.600107）日 K 线极值统计
统计A股每天上涨和下跌的股票数量。

按天为单位，收集每天的涨平和下跌的股票数量
[date, ups, downs]

"""

import pandas as pd
from pathlib import Path
from datetime import datetime

def read_all_stockfile(stock_file_dir = 'daily_data', start_id = 219, read_size = 10):
    dfs = []
    root = Path(stock_file_dir).resolve()          # 支持相对/绝对路径
    if not root.exists():
        print(f'{root} 目录不存在')
        return dfs
    
    csv_files = []
    for csv_file in root.rglob('*.csv'):
        csv_files.append(csv_file)
    total_count = len(csv_files)
    start_time = datetime.now()
    for i in range(total_count):
        if i < start_id:
            continue
        print(f'{str(datetime.now() - start_time)[:10]} {i:04d}/{total_count:04d} processing {csv_files[i]}', end  = '  ')
        try:
            df = pd.read_csv(csv_files[i], parse_dates=['date'])
            df.index = df.date
            df = df[df['date'] >= '2020-01-01']
            dfs.append(df)
            print(f'O')
        except Exception as e:
            print(f'X')
        if i >= start_id + read_size:
            break
    return dfs
 
stock_code = 'sh.600107'
# date	    code	    open	high	low	    close	preclose	volume	amount	adjustflag	turn	t   radestatus	pctChg	    isST
# 2018/1/5	sh.603161	24.12	24.12	24.12	24.12	16.75	    21910	528469	    3	    0.065599	1	        44.00001	0

def process_dfs(dfs):
    # 先筛选数据，只处理2020年1月1日以后的。
    for df in dfs:
        df = df[df['date'] >= '2020-01-01']
    
    
    
    # 4. 汇总结果 ---------------------------------------------------------
    return [ [date, ups, downs, ups/(ups+downs)]  ]
    
#if not file.exists('dfs.csv'):
dfs = read_all_stockfile()


print('reading done.')
print(len(dfs), dfs[0])

process_dfs(dfs)


